package com.cema.manage.project.android.biz;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alipay.api.AlipayApiException;
import com.cema.manage.common.exception.BizException;
import com.cema.manage.common.exception.RiskException;
import com.cema.manage.project.android.TestBaseContrller;
import com.cema.manage.project.android.model.PageVo;
import com.cema.manage.project.android.model.UserDailyActivityVo;
import com.cema.manage.project.android.model.WithdrawalRecordVo;
import com.cema.manage.project.android.result.JsonResult;
import com.cema.manage.project.android.service.BizWithdrawalService;
import com.cema.manage.project.manage.userDailyActivity.service.IUserDailyActivityService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("withdrawal")
public class BizWithdrawalController extends TestBaseContrller {

    @Resource
    private BizWithdrawalService bizWithdrawalService;

    @Resource
    private IUserDailyActivityService iUserDailyActivityService;

    @PostMapping("config")
    public JsonResult queryWithdrawalConfig(HttpServletRequest request) {
        String type = request.getParameter("type");
        String uvChannel = request.getParameter("uvChannel");
        return JsonResult.returnResult(JsonResult.SUCCESS, bizWithdrawalService.queryWithdrawalConfig(type,uvChannel));
    }

    @PostMapping("user")
    public JsonResult userWithdrawal(HttpServletRequest request, WithdrawalRecordVo withdrawalRecordVo) {
        try {
            if (StrUtil.isEmpty(withdrawalRecordVo.getOaid())) {
                return JsonResult.returnResetLogin("登录失效，请重新登录");
            }
            if (!getUser(request).getOaid().equals(withdrawalRecordVo.getOaid())) {
                throw new RiskException("操作异常，请重新登录");
            }
            iUserDailyActivityService.checkDeviceInfo(getUser(request), BeanUtil.toBean(withdrawalRecordVo, UserDailyActivityVo.class));
            bizWithdrawalService.userWithdrawal(getUser(request), withdrawalRecordVo);
        } catch (RiskException e) {
            e.printStackTrace();
            return JsonResult.returnResetLogin(e.getMessage());
        } catch (BizException e) {
            e.printStackTrace();
            return JsonResult.returnFail(e.getMessage());
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return JsonResult.returnSuccess("提交成功");
        }
        return JsonResult.returnSuccess("提交成功");
    }

    @PostMapping("list")
    public JsonResult query(HttpServletRequest request, PageVo pageVo) {
        return JsonResult.returnResult(JsonResult.SUCCESS, bizWithdrawalService.getUserWithdrawla(getUser(request).getId(), pageVo));
    }
}
